package it.unisa.indianamas;

import de.micromata.opengis.kml.v_2_2_0.AltitudeMode;
import de.micromata.opengis.kml.v_2_2_0.ColorMode;
import de.micromata.opengis.kml.v_2_2_0.Document;
import de.micromata.opengis.kml.v_2_2_0.Kml;
import de.micromata.opengis.kml.v_2_2_0.KmlFactory;
import de.micromata.opengis.kml.v_2_2_0.Placemark;
import de.micromata.opengis.kml.v_2_2_0.Polygon;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
 * Hello world!
 *
 */



public class App 
{
    
    
    public static void main( String[] args ) throws FileNotFoundException
    {
       // System.out.println( "Hello World!" );
        final Kml kml = KmlFactory.createKml();
        final Document document = kml.createAndSetDocument().withName("Polygon.kml").withOpen(false);
        //PolyStyle style1 = document.createAndAddStyle().withId("examplePolyStyle").createAndSetPolyStyle().withColor("ff0000cc").withColorMode(ColorMode.RANDOM);

        
        
        double x1 = 44.048852;
        double y1 = 7.443155;
        double z= 2500;

        List<Layer> layers = new ArrayList<Layer>();
        layers.add(new Layer(60,"0000cc"));
        layers.add(new Layer(90,"00ffcc"));
        layers.add(new Layer(150,"ffffcc"));
        drawColumn(document,  x1,  y1, (int)z, layers);
        
        double x2 = 44.055252;
        double y2 = 7.4435320;

        
        List<Layer> layers2 = new ArrayList<Layer>();
        layers2.add(new Layer(120,"0000cc"));
        layers2.add(new Layer(90,"00ffcc"));
        layers2.add(new Layer(30,"ffffcc"));
        drawColumn(document,  x2,  y2, (int)z, layers2);
       /* 
        double x3 = 44.065258;
        double y3 = 7.434043;

        
        List<Layer> layers3 = new ArrayList<Layer>();
        layers3.add(new Layer(120,"0000cc"));
        layers3.add(new Layer(120,"00ffcc"));
        layers3.add(new Layer(150,"ffffcc"));
        drawColumn(document,  x3,  y3, (int)z, layers3);
        
        double x4 = 44.063290;
        double y4 = 7.433616;
        
        List<Layer> layers4 = new ArrayList<Layer>();
        layers4.add(new Layer(80,"0000cc"));
        layers4.add(new Layer(140,"00ffcc"));
        layers4.add(new Layer(50,"ffffcc"));
        drawColumn(document,  x4,  y4, (int)z, layers4);
        */

        double x4 = 44.059013;
        double y4 = 7.430516;
        double stepx=0.000250;
        double stepy=0.000150;
        Random generator2 = new Random( 200 );
        for (int k=0; k < 40; k++) {
            
            int r1 = generator2.nextInt(200);
            int r2 = generator2.nextInt(200);
            int r3 = generator2.nextInt(200);
            List<Layer> layers4 = new ArrayList<Layer>();
            layers4.add(new Layer(r1,"0000cc"));
            layers4.add(new Layer(r2,"00ffcc"));
            layers4.add(new Layer(r3,"ffffcc"));

            drawColumn(document,  x4,  y4, (int)z, layers4);
            x4+=stepx;
            y4+=stepy;
        }
        
      //  kml.marshal(System.out);
       kml.marshal(new File("HelloKml.kml"));
    }
    
    
    public static void drawColumn(Document document, double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, int base, List<Layer> layers) {
        int bottom = base;
        for (Layer layer: layers) {
            drawPolygon( document,  x1,  y1,  x2,  y2,  x3,  y3,  x4,  y4,  bottom, layer.getHeight(), layer.getColor());
            bottom+=layer.getHeight();
        }
    }
    
    public static void drawColumn(Document document, double x1, double y1, int base, List<Layer> layers) {
        int bottom = base;
        for (Layer layer: layers) {
            drawPolygon( document,  x1,  y1,  bottom, layer.getHeight(), layer.getColor());
            bottom+=layer.getHeight();
        }
    }
    
    public static void drawPolygon(Document document, double x1, double y1, double z, double h, String color) {
        String transparency = KML.transparency;
        color =transparency+color;
        
        double deep = KML.DEEP;
        double width = KML.WIDTH;
        
        double x2 = x1+deep; //37.819267;
        double y2 = y1; //-122.3652;
                
        double x3 = x1+deep; //37.819861;
        double y3 = y1+width;//-122.3656;

        
        double x4 = x1; //37.819429;
        double y4 = y1+width; //-122.3666;
        drawPolygon( document,  x1,  y1,  x2,  y2,  x3,  y3,  x4,  y4,  z, h, color);
    }
    public static void drawPolygon(Document document, double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double z, double h, String color) {

        Placemark pma = document.createAndAddPlacemark().withName("hollow box a");
        Polygon polygona = pma.createAndSetPolygon().withExtrude(false).withAltitudeMode(AltitudeMode.ABSOLUTE);
        pma.createAndAddStyle().withId("examplePolyStyle").createAndSetPolyStyle().withColor(color).withColorMode(ColorMode.NORMAL);
        polygona.createAndSetOuterBoundaryIs().createAndSetLinearRing()
            .addToCoordinates(y1,x1,z)
            .addToCoordinates(y2,x2,z)
            .addToCoordinates(y3, x3, z)
            .addToCoordinates(y4, x4, z)
            .addToCoordinates(y1, x1, z);
        
        Placemark pmb = document.createAndAddPlacemark().withName("hollow box b");
        Polygon polygonb = pmb.createAndSetPolygon().withExtrude(false).withAltitudeMode(AltitudeMode.ABSOLUTE);
        pmb.createAndAddStyle().withId("examplePolyStyle").createAndSetPolyStyle().withColor(color).withColorMode(ColorMode.NORMAL);
        polygonb.createAndSetOuterBoundaryIs().createAndSetLinearRing()
            .addToCoordinates(y1,x1,z)
            .addToCoordinates(y4,x4,z)
            .addToCoordinates(y4, x4, z+h)
            .addToCoordinates(y1, x1, z+h)
            .addToCoordinates(y1, x1, z);

        Placemark pmc = document.createAndAddPlacemark().withName("hollow box c");
        Polygon polygonc = pmc.createAndSetPolygon().withExtrude(false).withAltitudeMode(AltitudeMode.ABSOLUTE);
        pmc.createAndAddStyle().withId("examplePolyStyle").createAndSetPolyStyle().withColor(color).withColorMode(ColorMode.NORMAL);
        polygonc.createAndSetOuterBoundaryIs().createAndSetLinearRing()
            .addToCoordinates(y1,x1,z)
            .addToCoordinates(y2,x2,z)
            .addToCoordinates(y2, x2, z+h)
            .addToCoordinates(y1, x1, z+h)
            .addToCoordinates(y1, x1, z);
        
        Placemark pmd = document.createAndAddPlacemark().withName("hollow box d");
        Polygon polygond = pmd.createAndSetPolygon().withExtrude(false).withAltitudeMode(AltitudeMode.ABSOLUTE);
        pmd.createAndAddStyle().withId("examplePolyStyle").createAndSetPolyStyle().withColor(color).withColorMode(ColorMode.NORMAL);
        polygond.createAndSetOuterBoundaryIs().createAndSetLinearRing()
            .addToCoordinates(y2,x2,z)
            .addToCoordinates(y2,x2,z+h)
            .addToCoordinates(y3, x3, z+h)
            .addToCoordinates(y3, x3, z)
            .addToCoordinates(y2, x2, z);

        Placemark pme = document.createAndAddPlacemark().withName("hollow box e");
        Polygon polygone = pme.createAndSetPolygon().withExtrude(false).withAltitudeMode(AltitudeMode.ABSOLUTE);
        pme.createAndAddStyle().withId("examplePolyStyle").createAndSetPolyStyle().withColor(color).withColorMode(ColorMode.NORMAL);
        polygone.createAndSetOuterBoundaryIs().createAndSetLinearRing()
            .addToCoordinates(y1,x1,z+h)
            .addToCoordinates(y2,x2,z+h)
            .addToCoordinates(y3, x3, z+h)
            .addToCoordinates(y4, x4, z+h)
            .addToCoordinates(y1,x1,z+h);        
        
                Placemark pmf = document.createAndAddPlacemark().withName("hollow box f");
        Polygon polygonf = pmf.createAndSetPolygon().withExtrude(false).withAltitudeMode(AltitudeMode.ABSOLUTE);
        pmf.createAndAddStyle().withId("examplePolyStyle").createAndSetPolyStyle().withColor(color).withColorMode(ColorMode.NORMAL);
        polygonf.createAndSetOuterBoundaryIs().createAndSetLinearRing()
            .addToCoordinates(y3,x3,z)
            .addToCoordinates(y3,x3,z+h)
            .addToCoordinates(y4, x4, z+h)
            .addToCoordinates(y4, x4, z)
            .addToCoordinates(y3,x3,z);   

}
}
